<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, getEditorTitle,changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				ratioIdKey:'obj-year-tinstAnnualTargetPlan-title_indexDecompose-ratioId-cache',
				orgCacheKey:'obj-year-tinstAnnualTargetPlan-title_indexDecompose-inst-cache',
				dutyOffCache:'obj-year-tinstAnnualTargetPlan-title_indexDecompose-dutyOff-cache',
				indexcache:"obj-year-tinstAnnualTargetPlan-title_indexDecompose-index-cache",
				index1cache:"obj-year-tinstAnnualTargetPlan-title_indexDecompose-index1-cache",
				firstOrder:100
			})
		},
		methods:{
			clearIndexId(){
				let _that = this
				_that.$refs.autoform.clear("title_indexDecompose","indexId")
				_that.$refs.autoform.clear("title_indexDecompose","rangeId")
				_that.$refs.autoform.clear("title_indexDecompose","dutyOffId")
				_that.$refs.autoform.clear("title_indexDecompose","dutyInstName")
				_that.$refs.autoform.clear("title_indexDecompose","targetValueCode")
				_that.$refs.autoform.clear("title_indexDecompose","challengeTargetCode")
				_that.$refs.autoform.clear("title_indexDecompose","ratioId")
				_that.$refs.autoform.clear("title_indexDecompose","checksId")
				_that.$refs.autoform.clear("title_indexDecompose","weight")
				_that.$refs.autoform.clear("title_indexDecompose","remark")
				_that.$refs.autoform.clear("title_indexDecompose","indexDefine")
				_that.$refs.autoform.clear("title_indexDecompose","indexAttrName")
				_that.$refs.autoform.clear("title_indexDecompose","indexCode1")
			},
			inputWeight(val){
				let _that = this
				if(Number(val) > 100){
					_that.$refs.autoform.formdata.weight = 100
				}else if(Number(val) < 1){
					_that.$refs.autoform.formdata.weight = 1
				}
			},
			inputTargetValue(val){
				let _that = this
				_that.$refs.autoform.formdata.targetValueName = _that.$refs.autoform.formdata.logicRelationshipName + val + _that.$refs.autoform.formdata.indexUnitName
				_that.$refs.autoform.formdata.targetValueName_ = _that.$refs.autoform.formdata.logicRelationshipName + val + _that.$refs.autoform.formdata.indexUnitName
			},
			verifyDutyOff(){
				let _that = this
				if(!_that.$refs.autoform.formdata.indexId){
					let title = _that.$refs.autoform.getEditorTitle("title_indexDecompose","indexId")
					_that.om.toast('请先选择'+title)
					return false
				}else{
					return true
				}
			},
			changeType(logicRelationshipName,indexUnitName){
				let _that = this
				_that.$refs.autoform.formdata.logicRelationshipName = logicRelationshipName
				_that.$refs.autoform.formdata.indexUnitName = indexUnitName
				_that.$refs.autoform.changeEditor("title_indexDecompose", "targetValueCode","required",true)
				_that.$refs.autoform.changeEditor("title_indexDecompose","targetValueCode","type","numberbox")
				_that.$refs.autoform.changeEditor("title_indexDecompose","targetValueCode","prefix",logicRelationshipName)
				_that.$refs.autoform.changeEditor("title_indexDecompose","targetValueCode","suffix",indexUnitName)
				_that.$refs.autoform.changeEditor("title_indexDecompose", "targetValueCode", "onSelect", 'inputTargetValue')
				
				_that.$refs.autoform.changeEditor("title_indexDecompose","challengeTargetCode","type","numberbox")
				_that.$refs.autoform.changeEditor("title_indexDecompose","challengeTargetCode","suffix",indexUnitName)
				_that.$refs.autoform.changeEditor("title_indexDecompose","challengeTargetCode","prompt",'请输入')
				_that.$refs.autoform.changeEditor("title_indexDecompose","remark1","type","textarea")
				_that.$refs.autoform.changeEditor("title_indexDecompose","weight","type","numberbox")
			},
			checkIndexAttrId(id){
				let _that = this
				if(id == "36583"){
					let arr = _that.datas.filter((e)=>{
						if((e.orderNum != _that.$refs.autoform.formdata.orderNum) && (e.indexAttrCode != "36583")){
							return e
						}
					})
					// let arr = _that.datas.filter((e)=>e.orderNum != _that.$refs.autoform.formdata.orderNum && (_that.$refs.autoform.formdata.indexAttrCode != "36583"))
					uni.setStorage({
						key:_that.index1cache,
						data:arr,
						success:(res)=>{
							_that.$refs.autoform.changeEditor("title_indexDecompose","indexCode1","type","linkbox")
							let title = _that.$refs.autoform.getEditorTitle("title_indexDecompose","indexId")
							_that.$refs.autoform.changeEditor("title_indexDecompose","indexCode1",{
								link:"../../public/search/search",
								"linkParam":{
									other:JSON.stringify({}),
									cachename:_that.indexcache,
									otherCache:_that.index1cache,
									defaultData:true,
									initload:true,
									namefield:"indexName",
									extrafield:'orderNum',
									placeholder:"请输入"+title
								}
							})
						}
					})
				}else{
					_that.$refs.autoform.changeEditor("title_indexDecompose","indexCode1","type","labelbox")
				}
			},
			getIndexDefine(id){
				let _that = this
				_that.om.request(
					"indexDict/getIndexDefine",
					{
						indexId:id
					}
				).then(res=>{
					_that.$refs.autoform.formdata.indexDefine = res.value
					// _that.$refs.autoform.changeEditor("title_indexDecompose","indexDefine","type","lookfilebox")
					// _that.$refs.autoform.changeEditor("title_indexDecompose","indexDefine","isText",true)
					// _that.$refs.autoform.changeEditor("title_indexDecompose","indexDefine","remark",res.value||'')
					// _that.$refs.autoform.changeEditor("title_indexDecompose","indexDefine","disabled",true)
					// _that.$refs.autoform.changeEditor("title_indexDecompose","indexDefine","showClear",false)
				})
			},
			selectChecksId(data){
				let _that = this
				_that.$refs.autoform.changeEditor("title_indexDecompose", "weight", "type", "numberbox")
			},
			clearChecksId(){
				let _that = this
				_that.$refs.autoform.clear("title_indexDecompose","checksId")
				_that.$refs.autoform.clear("title_indexDecompose","weight")
			},
			getCheckList(){
				let _that = this
				_that.om.request(
					"torgAnnualTargetPlan/getCheckList",
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_indexDecompose","checksId","type","selectbox")
					_that.$refs.autoform.changeEditor("title_indexDecompose","checksId","data",configSelectData(res,"id","text"))
				})
			},
			getIndexUserList(id){
				let _that = this
				_that.$refs.autoform.changeEditor("title_indexDecompose","dutyOffId","type","linkbox")
				_that.$refs.autoform.changeEditor("title_indexDecompose","dutyOffId",{
					link:"../../public/search/search",
					"linkParam":{
						other:JSON.stringify({
							instId: _that.instId,
							indexId: id
						}),
						url:"torgMediumTermPlan/getIndexUserList",
						namefield:"userPosName",
						extrafield:'instName',
						initload:true,
						cachename:_that.dutyOffCache
					}
				})
			},
			getCategoryList(id){
				let _that = this
				_that.om.request(
					"indexCategory/findIndexCategoryList",
					{
						indexDictId:id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_indexDecompose",'rangeId','type','selectbox')
					_that.$refs.autoform.changeEditor("title_indexDecompose",'rangeId',"data",configSelectData(res,"indexCategoryId", "indexCategoryName"))
				})
			},
			getRatio() {
				let _that = this
				_that.$refs.autoform.changeEditor("title_indexDecompose", "ratioId", "type", "linkbox")
				_that.$refs.autoform.changeEditor("title_indexDecompose", "ratioId", {
					link: "../../public/search/search",
					"linkParam": {
						other: JSON.stringify({
							
						}),
						url: "dataDict/getCombobox.do?code=DD021001",
						namefield: "dicname",
						extrafield: "initNum",
						initload: true,
						cachename: _that.ratioIdKey,
						placeholder: "请输入"
					}
				})
			}
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.orgCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.indexId = d.id
							formdata.indexName = d.dicname
							formdata.indexAttrCode = d.indexAttrCode
							formdata.indexAttrName = d.indexAttrName
							formdata.weight = d.power
							_that.checkIndexAttrId(d.indexAttrCode)
							_that.getCategoryList(d.id)
							_that.getIndexUserList(d.id)
							_that.getCheckList()
							_that.getRatio()
							_that.getIndexDefine(d.id)
							let pArr = []
							let rArr = []
							let objFormCode = JSON.parse(uni.getStorageSync('objFormCode'))
							// if(d.indexAttrCode == "36583"){
							// 	if(_that.datas.length ==0){
							// 		formdata.orderNum = objFormCode.DICT_CODE_INDEX_P+1
							// 	}else{
							// 		for(let item of _that.datas){
							// 			if(item.orderNum.indexOf(objFormCode.DICT_CODE_INDEX_P) != -1 ){
							// 				pArr.push(item)
							// 				let num = pArr[pArr.length-1].orderNum.match(/\d+/g)
							// 				formdata.orderNum = objFormCode.DICT_CODE_INDEX_P+(Number(num)+1)
							// 			}else{
							// 				formdata.orderNum = objFormCode.DICT_CODE_INDEX_P+1
							// 			}
							// 		}
							// 	}
							// }else{
								if(_that.datas.length ==0){
										formdata.orderNum = objFormCode.DICT_CODE_R+1
									}else{
										for(let item of _that.datas){
											if(item.orderNum.indexOf(objFormCode.DICT_CODE_R)!= -1){
												rArr.push(item)
												let num = rArr[rArr.length-1].orderNum.match(/\d+/g)
												formdata.orderNum = objFormCode.DICT_CODE_R+(Number(num)+1)
											}else{
												formdata.orderNum = objFormCode.DICT_CODE_R+1
											}
										}
									}
								// }
								_that.changeType(d.logicRelationshipName,d.indexUnitName)
							uni.removeStorage({
								key:_that.orgCacheKey
							})
						}
					}
				}),
				uni.getStorage({
					key:_that.dutyOffCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.dutyOffId = d.id
							formdata.dutyOffName = d.userName
							formdata.dutyInstName = d.instName
							uni.removeStorage({
								key:_that.dutyOffCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.indexcache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.indexCode1 = d.orderNum
							uni.removeStorage({
								key:_that.indexcache
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
			if(e.configDataCacheKey){
				_that.instId = e.instId
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							let title = getEditorTitle("title_indexDecompose","indexId",_that.autoformdata)
								changeEditor("title_indexDecompose","indexId",{
									link:"../../public/search/search",
									"linkParam":{
										other:JSON.stringify({
											instId:_that.instId
										}),
										url:"indexDict/getAllIndex2",
										namefield:"dicname",
										extrafield:"indexUnitName",
										notefield:"logicRelationshipName",
										initload:true,
										cachename:_that.orgCacheKey,
										placeholder:"请输入"+title
									}
								},null,_that.autoformdata)
							}
						}
					})
				}
				_that.$nextTick(function(){
					if(_that.$refs.autoform.formdata.indexId){
						_that.checkIndexAttrId(_that.$refs.autoform.formdata.indexAttrCode)
						_that.getCategoryList(_that.$refs.autoform.formdata.indexId)
						_that.getIndexUserList(_that.$refs.autoform.formdata.indexId)
						_that.getCheckList()
						_that.getRatio()
						_that.getIndexDefine(_that.$refs.autoform.formdata.indexId)
						_that.changeType(_that.$refs.autoform.formdata.logicRelationshipName,_that.$refs.autoform.formdata.indexUnitName)
					}
				})
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
